﻿/******************************************************************************
 * Name         : Console.hpp
 * Author       : 671643387
 * Date         : 2015年9月23日
 * Description  :
 *****************************************************************************/

#ifndef ANIO_BASE_CONSOLE_HPP_
#define ANIO_BASE_CONSOLE_HPP_

#include <iostream>

#if defined(__WINDOWS__)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif

namespace dev
{
    namespace base
    {
#if defined(__LINUX__)
        const static int CONSOLE_COLOR_FOREGROUND_INTENSITY =   0x1;
        const static int CONSOLE_COLOR_FOREGROUND_BLUE =        0x2;
        const static int CONSOLE_COLOR_FOREGROUND_GREEN =       0x4 ;
        const static int CONSOLE_COLOR_FOREGROUND_RED =         0x8;
        const static int CONSOLE_COLOR_FOREGROUND_YELLOW = CONSOLE_COLOR_FOREGROUND_RED | CONSOLE_COLOR_FOREGROUND_GREEN;
        const static int CONSOLE_COLOR_FOREGROUND_WHITE = CONSOLE_COLOR_FOREGROUND_RED | CONSOLE_COLOR_FOREGROUND_GREEN | CONSOLE_COLOR_FOREGROUND_BLUE;

        /**
         * @brief 设置控制台前景色
         * @param forecolor 前景色(默认白色不高亮)
         */
        inline void set_console_color(unsigned short forecolor = CONSOLE_COLOR_FOREGROUND_WHITE & ~CONSOLE_COLOR_FOREGROUND_INTENSITY)
        {
            // 重置状态
            std::cout << "\033[0m";

            // 设置高亮
            if ((forecolor & CONSOLE_COLOR_FOREGROUND_INTENSITY) != 0)
            {
                std::cout << "\033[1m";
            }

            forecolor &= ~CONSOLE_COLOR_FOREGROUND_INTENSITY;

            // 设置前景色
            switch (forecolor)
            {
            case CONSOLE_COLOR_FOREGROUND_BLUE:
                std::cout << "\033[34m";
                break;
            case CONSOLE_COLOR_FOREGROUND_GREEN:
                std::cout << "\033[32m";
                break;
            case CONSOLE_COLOR_FOREGROUND_RED:
                std::cout << "\033[31m";
                break;
            case CONSOLE_COLOR_FOREGROUND_YELLOW:
                std::cout << "\033[33m";
                break;
            case CONSOLE_COLOR_FOREGROUND_WHITE:
                std::cout << "\033[37m";
                break;

            default:
                break;
            }
        }
#elif defined(__WINDOWS__)

        const static int CONSOLE_COLOR_FOREGROUND_INTENSITY = FOREGROUND_INTENSITY;
        const static int CONSOLE_COLOR_FOREGROUND_BLUE = FOREGROUND_BLUE;
        const static int CONSOLE_COLOR_FOREGROUND_GREEN = FOREGROUND_GREEN ;
        const static int CONSOLE_COLOR_FOREGROUND_RED = FOREGROUND_RED;
        const static int CONSOLE_COLOR_FOREGROUND_YELLOW = FOREGROUND_RED | FOREGROUND_GREEN;
        const static int CONSOLE_COLOR_FOREGROUND_WHITE = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;

        /**
         * @brief 设置控制台前景色
         * @param forecolor 前景色(默认白色不高亮)
         */
        inline void set_console_color(unsigned short forecolor = CONSOLE_COLOR_FOREGROUND_WHITE & ~CONSOLE_COLOR_FOREGROUND_INTENSITY)
        {
            HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
            SetConsoleTextAttribute(hCon, forecolor | 0);
        }
#endif
    }
}

#endif /* ANIO_BASE_CONSOLE_HPP_ */
